package cn.jinjimi.app.main.fragment;

import android.content.Intent;
import android.graphics.Point;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import cn.jinjimi.framework.helper.annotation.EventTrace;
import cn.jinjimi.framework.helper.annotation.EventTraceParam;
import cn.jinjimi.app.R;
import cn.jinjimi.app.bean.EventDataField;
import cn.jinjimi.app.bean.EventID;
import cn.jinjimi.app.main.activity.MainActivity;
import cn.jinjimi.app.main.adapter.pagerAdapter.CommonPageAdapter;
import cn.jinjimi.app.main.extend.BaseFragment;
import cn.jinjimi.app.main.widget.FrescoImageView.FrescoImageView;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by HANER on 15/4/13.
 */
public class SplashFragment extends BaseFragment implements View.OnClickListener {
    private ViewPager viewPager;
    private List<View> imgsList;
    private ImageView[] points;

    /* 只需在此处定义图片数量即可完成更新启动图 */
    private int[] imgRes = {
            R.drawable.logo,
            R.drawable.logo,
            R.drawable.logo,
            R.drawable.logo
    };

    @Override
    protected int getLayoutResourceId() {
        return R.layout.fragment_splash;
    }

    @Override
    protected void initial() {
        initView();
        initListener();
        initData();
    }

    protected void initView() {
        viewPager = (ViewPager) findView().findViewById(R.id.vp_images);
        LinearLayout viewgroup = (LinearLayout) findView().findViewById(R.id.viewGroup);
        //创建导航图标
        points = new ImageView[imgRes.length];
        imgsList = new ArrayList<>();
        //创建图片组
        for (int i = 0; i < imgRes.length; i++) {

            FrescoImageView img = new FrescoImageView(getActivity());
            img.setPlaceHolderImage(imgRes[i]);
            imgsList.add(img);

            points[i] = new ImageView(getActivity());
            points[i].setBackgroundResource(R.drawable.point_blue_on);
            if (i != 0) {
                points[i].setBackgroundResource(R.drawable.point_blue_normal);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                params.setMargins(getActivity().getResources().getDimensionPixelOffset(R.dimen.dp_10), 0, 0, 0);
                points[i].setLayoutParams(params);
            }
            viewgroup.addView(points[i]);

        }
        //给最后一张图片添加点击事件
        //点击该图片后跳转到下一个界面
        imgsList.get(imgsList.size() - 1).setOnClickListener(this);

//        Log.e("ViewHeight", "" + viewPager.getHeight());
//        Log.e("ViewWidth", "" + viewPager.getWidth());
//        Log.e("ScreenHeight", "" + AndroidUtils.getWindowHeight(getActivity()));
//        Log.e("ScreenWidth", "" + AndroidUtils.getWindowWidth(getActivity()));
//        Log.e("another", "不包含虚拟键的高度=" + getNoHasVirtualKey());
//        Log.e("another", "包含虚拟键的高度=" + getHasVirtualKey());
//        Log.e("another", "虚拟键的高度=" + (getHasVirtualKey() - getNoHasVirtualKey()));

        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) viewgroup.getLayoutParams();
        params.setMargins(0, 0, 0, (getHasVirtualKey() - getNoHasVirtualKey() + getActivity().getResources().getDimensionPixelOffset(R.dimen.dp_20)));
        viewgroup.setLayoutParams(params);
    }

    protected void initListener() {
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            private boolean isLastPager = false;
            private int trys = 0;

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            @EventTrace(EventID.APP_ELUNCH_INDEX)
            public void onPageSelected(@EventTraceParam(value = EventDataField.ParamId, isValueAutoPlusOne = true) int position) {
                isLastPager = viewPager.getAdapter().getCount() == position + 1;

                //更新导航点状态
                int index = position % points.length;
                points[index].setBackgroundResource(R.drawable.point_blue_on);
                for (int i = 0; i < points.length; i++) {
                    if (index != i) {
                        points[i].setBackgroundResource(R.drawable.point_blue_normal);
                    }
                }

            }

            @Override
            public void onPageScrollStateChanged(int state) {
                if (isLastPager && state == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
                    //LogUtil.d(TAG, "the last item");
                    if (trys > 0)
                        //LogUtil.d(TAG, "Go to next Activity");
                        direct();
                    else
                        trys++;
                }
            }
        });
    }

    protected void initData() {
        viewPager.setAdapter(new CommonPageAdapter(imgsList));
    }

    @Override
    public void onClick(View v) {
        direct();
    }

    /* 显示App介绍页面之后跳转到不同的Activity */
    private void direct() {
        //统计

        Intent intent = new Intent(getActivity(), MainActivity.class);
        startActivity(intent);
        getActivity().finish();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        viewPager.removeAllViews();
        viewPager.setAdapter(null);
        viewPager = null;
        System.gc();
    }

    public int getNoHasVirtualKey() {
        Point point = new Point();
        getActivity().getWindowManager().getDefaultDisplay().getSize(point);
        return point.y;
    }

    private int getHasVirtualKey() {
        int dpi = 0;
        Display display = getActivity().getWindowManager().getDefaultDisplay();
        DisplayMetrics dm = new DisplayMetrics();
        @SuppressWarnings("rawtypes")
        Class c;
        try {
            c = Class.forName("android.view.Display");
            @SuppressWarnings("unchecked")
            Method method = c.getMethod("getRealMetrics", DisplayMetrics.class);
            method.invoke(display, dm);
            dpi = dm.heightPixels;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dpi;
    }
}
